<?php
namespace app\controller;

use app\controller\Base;
use app\model\UserModel;
use think\facade\Db;
use support\Request;
use support\View;
class XiaController extends Base
{
    public function config(Request $request){

        $path = public_path().'/update';
        $arr=[];
        $files = scandir($path);
        foreach ($files as $file) {
            if ($file != '.' && $file != '..') {
                $hou=pathinfo($file);
                if(isset($hou['extension']) and $hou['extension']=='apk'){
                    $arr[]= $hou['filename'];
                }
            }
        }
        rsort($arr);
        if(empty($arr)){
            e('最新版本无需升级');
        }
        $url='http://'.$request->host();
        $res2=[
            'downloadUrl'=>$url.'/update/'.$arr[0].'.apk',//apk静态下载地址 将apk文件名修改为download.apk替换static/apk目录下的文件
            'name'=> '省钱商城', //App名称
            'sortName'=> '省钱商城', //简称
            'slogan'=> '真正能为您开启省钱之旅的 APP', //标语
            'qrcodeUrl'=> $url.'/appConfig/ewm.png', //展示的二维码
            'logo'=> $url.'/appConfig/logo.png', //applogo
            'copyright'=> '省钱商城', //版权所有
        ];
        s($res2);
    }
    public function index(Request $request)
    {

        $mall_config=Db::name('mall_config')->column('value','key');
        $last=Db::name('mall_update')->where('isopen',1)->order('app_v desc')->find();
		return view('xia/index',[
			'app_v'=>$last['app_v'],
			'app_desc'=>$mall_config['app_desc'],
			'app_name'=>$mall_config['app_name'],
            'app_logo'=> $mall_config['app_logo']
		]);
    }
    public function ajax(Request $request){

        $data=$request->all();
        if($data['type']=='get_yzm'){
            $msg=mt_rand(111111, 999999);
            $user=new UserModel();
            if ($this->sendsmg($data['phone'], $msg)) {
                if ($u=Db::name('user')->where('phone', $data['phone'])->find()) {
                    $user->where('id', $u['id'])->update([
                        'yzm'=>$msg
                    ]);
                } else {
                    $user->save([
                        'phone'=>$data['phone'],
                        'yzm'=>$msg,
                    ]);
                }
                return json(['code'=>1,'message'=>'成功']);
            } else {
                return json(['code'=>0,'message'=>'发送验证码失败']);
            }
        }
    }
    public function main(Request $request)
    {
          if(request()->method()=='POST'){
            $data=$request->post();
            if(!isset($data['code'])){
                return view_e('邀请码不正确');
            }
            if(!isset($data['password']) or !$data['password']){
                return view_e('请填写密码');
            }
            if($data['password']!=$data['password2']){
                return view_e('两次密码不一致');
            }
            $f_user=Db::name('user')->where('user_no',$data['code'])->find();
            if(!$f_user){
                return view_e('邀请码不正确');
            }
            if ($wo=Db::name('user')->where('phone', $data['phone'])->where('yzm',$data['yzm'])->where('isopen',1)->find()) {
                if (!$wo['user_no']) {
                    Db::name('user')->where('id', $wo['id'])->update(['user_no'=>$this->sui($wo['id'])]);
                }
                if (!$wo['fid']) {
                    Db::name('user')->where('id', $wo['id'])->update(['fid'=>$f_user['id']]);
                }
                Db::name('user')->where('id', $wo['id'])->update(['password'=>md5(sha1($data['password']))]);
                return view_s('注册成功，请下载app',url('xia/index',['st'=>1]));
            } else {
                return view_e('注册失败了');
            }
        }
        $mall_config=Db::name('mall_config')->column('value','key');
        $last=Db::name('mall_update')->where('isopen',1)->order('app_v desc')->find();
        return view('',[
            'app_v'=>$last['app_v'],
            'app_desc'=>$mall_config['app_desc'],
            'app_name'=>$mall_config['app_name'],
            'app_logo'=> $mall_config['app_logo']
        ]);
    }
    public function shuoming(Request $request){
        return view('',[
            'data'=>Db::name('shuoming')->where('id',input('id'))->find()
        ]);
    }
}
